package tc.jv.task6;

//import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import tc.jv.task4.Vector;

public class Main
{
	
	private static String CURRENT_PATH = "E:\\workspace\\NetCracker\\src\\tc\\jv\\task5\\";
//	private static String CURRENT_PATH = "D:\\Documents\\NC\\jv\\task5\\";
//	private static String CURRENT_PATH = "C:\\Users\\netcracker\\workspace\\NetCracker\\src\\jv\\task5\\";
	

	public static void main(String[] args)
	{
		System.out.println(System.getProperty("user.dir"));
//		byteStreams();
//		charStreams();
		serialization();
		
	}

	public static void byteStreams()
	{
		System.out.println("Byte streams");
	
		String filename = "test.bvc";
		
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try
		{
			System.out.println("Output to file:");
			fos = new FileOutputStream(CURRENT_PATH + filename);
			Vectors.outputVector(Vectors.generateRandomVector(5, 0, 5), fos);
			fos.flush();
			Vectors.outputVector(Vectors.generateRandomVector(5, 0, 5), fos);
			fos.flush();
			
			System.out.println("Input from file:");
			fis = new FileInputStream(CURRENT_PATH + filename);
			Vectors.inputVector(fis).print();
			Vectors.inputVector(fis).print();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try {fos.close(); fis.close();} catch (IOException e) {e.printStackTrace();}
		}
		
		System.out.println("Output to \"System.out\":");
		Vectors.outputVector(Vectors.generateRandomVector(5, 0, 5), System.out);
		System.out.println();
		
//		System.out.println("Input from \"System.in\":");
////		Vector v = Vectors.inputVector(System.in);
////		v.print();
//		
//		int a = 42;
//		DataInputStream dis = new DataInputStream(System.in);
//		try
//		{
////			a = (int) dis.read();
//			int size = dis.readInt();
//			System.out.println(size);
//			JLinkedListVector v = new JLinkedListVector();
//			while (size-- > 0)
//				v.add(dis.readDouble());
//		}
//		catch (IOException e)
//		{
//			e.printStackTrace();
//		}
//		System.out.println(a == 42 ? "Not" : a);
	}
	
	public static void charStreams()
	{
		System.out.println("Character streams");
		
		String filename = "test.cvc";
		
		FileWriter fw = null;
		FileReader fr = null;
		try
		{
			System.out.println("Output to file:");
			fw = new FileWriter(CURRENT_PATH + filename);
			Vectors.writeVector(Vectors.generateRandomVector(5, 0, 5), fw);
			fw.flush();
			
			System.out.println("Input from file:");
			fr = new FileReader(CURRENT_PATH + filename);
			Vectors.readVector(fr).print();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try {fw.close(); fr.close();} catch (IOException e) {e.printStackTrace();}
		}
//		Vectors.writeVector(Vectors.generateRandomVector(5, 0, 5), );
	}

	public static void serialization()
	{
		System.out.println("Serialization:");
		
		String filename = "obj.vec";
		
		ObjectOutputStream out = null;
		ObjectInputStream in = null;
		
		try
		{
			int size = 5;
			out = new ObjectOutputStream(
					new FileOutputStream(
						CURRENT_PATH + filename));
			tc.jv.task7.Vectors.setVectorFactory(new tc.jv.task7.ArrayVectorFactory());
//			tc.jv.task7.Vectors.setVectorFactory(new tc.jv.task7.JArrayListVectorFactory());
//			tc.jv.task7.Vectors.setVectorFactory(new tc.jv.task7.LinkedListVectorFactory());
//			tc.jv.task7.Vectors.setVectorFactory(new tc.jv.task7.JLinkedListVectorFactory());
			out.writeObject(Vectors.generateRandomVector(size, 0, 5));
			
			Vector v;
			in = new ObjectInputStream(
					new FileInputStream(
						CURRENT_PATH + filename));
			v = (Vector) in.readObject();
			v.print(1);
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try{out.close(); in.close();} catch (IOException e)	{e.printStackTrace();}
		}
	}
	
}
